<?php

namespace App\Admin\Controllers;

use App\Models\InPressArticle;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use App\Models\Journal;

class InPressArticleController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('Index')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new InPressArticle);
        $grid->model()->orderBy('id', 'desc');
        $grid->id('ID')->sortable();
        $grid->title("标题");
        $grid->keywords('关键词');
        $grid->view_count("浏览量");
        $grid->download_count("下载量");
        $grid->publish_date('发表时间');
        $grid->filter(function($filter){
            $filter->like('title');
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            
            $actions->disableView();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(InPressArticle::findOrFail($id));

        $show->id('Id');
        $show->journal_id('Journal id');
        $show->title('Title');
        $show->title_cn('Title cn');
        $show->doi_number('Doi number');
        $show->abstract('Abstract');
        $show->abstract_cn('Abstract cn');
        $show->authors('Authors');
        $show->affiliation('Affiliation');
        $show->keywords('Keywords');
        $show->fund_info('Fund info');
        // $show->images('Images');
        $show->pdf('Pdf');
        $show->download_count('Download count');
        $show->view_count('View count');
        $show->is_online('Is online');
        $show->publish_date('Publish date');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new InPressArticle);

        $journals = Journal::all();
        $journalsList = [];
        foreach ($journals as $j) {
            $journalsList[$j->id] = $j['name_short'];
        }
        $form->select('journal_id', '期刊')->options($journalsList)->default(2)->rules('required');;
        $form->text('title', '标题')->rules('required');;
        $form->text('doi_number', 'Doi');
        $form->editor('abstract', '摘要');
        $form->editor('abstract_cn', '中文摘要');
        $form->editor('authors', '作者')->rules('required');;
        $form->editor('affiliation', '单位');
        $form->text('keywords', '关键词');
        $form->textarea('fund_info', '基金信息');
        // $form->text('images', 'Images');
        $form->file('pdf', 'PDF');
        $form->number('download_count', '下载量');
        $form->number('view_count', '浏览量');
        $states = [
            'on' => ['value' => 1, 'text' => 'Yes'],
            'off' => ['value' => 0, 'text' =>'No'],
        ];

        // $form->switch('is_online', '发表')->states($states);
        $form->date('publish_date', '发表时间')->default(date('Y-m-d'))->format('YYYY-MM-DD');
		
		$form->disableViewCheck();
        $form->disableEditingCheck();
        $form->tools(function(\Encore\Admin\Form\Tools $tools) {
            $tools->disableView();
        });
        return $form;
    }
}
